;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?ActivityUuid=") != -1) {
			var reqPath = basePath+'Activity/queryActivityById'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == '1')
				{
					if (res.data != null)
					{
						debugger;
						var Activity = res.data;
						$("#uu_id").val(Activity.ActivityUuid);
						$("#Activity_name").val(Activity.ActivityName);
						$("#Activity_shop").val(Activity.ActivityShop);
						$("#identityCard").val(Activity.identityCard);
						$("#Activity_telephone").val(Activity.ActivityTelephone);
						$("#Activity_phone").val(Activity.ActivityPhone);
						$("#province").val(Activity.province);
						$("#city").val(Activity.city);
						$("#county").val(Activity.county);
						$("#village").val(Activity.village);
						$("#address").val(Activity.address);
						$("#businessOurs").val(Activity.businessOurs);
						$("#longitude").val(Activity.longitude);
						$("#latitude").val(Activity.latitude);
						$("#Activity_num").val(Activity.ActivityNum);
						$("#business_volume").val(Activity.businessVolume);
						$("#Activity_email").val(Activity.ActivityEmail);
						$("#level").val(Activity.level);
						$("#Activity_type").val(Activity.ActivityType);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else if(url.indexOf("?type=add") != -1)
		{
			queryLocation(null);
			queryCity(null);
			queryActivityType(null);
			$('#start_date').datetimepicker();
		}else
		{
			//初始化列表
			initActivityData(1,15);
		}
		$('#addnew').click(function()
		{

			window.location.href = jumpPath + 'view/luckeys/activity/addActivity.jsp?type=add';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			initActivityData(1,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveLoginActivity();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateActivity();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function queryLocation(locationUuid)
{
	var requsetPath = basePath + "location/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == '1')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#location_uuid").append("<option value='"+n.locationUuid+"'>"+n.locationName+"</option>");
			});
			if(null!=locationUuid)
			{
				$("#location_uuid").val(locationUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryCity(cityUuid)
{
	var requsetPath = basePath + "city/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == '1')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#city_uuid").append("<option value='"+n.cityUuid+"'>"+n.cityName+"</option>");
			});
			if(null!=cityUuid)
			{
				$("#city_uuid").val(cityUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryActivityType(activityTypeUuid)
{
	var requsetPath = basePath + "activityType/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == '1')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#activityType_uuid").append("<option value='"+n.activityTypeUuid+"'>"+n.activityTypeName+"</option>");
			});
			if(null!=activityTypeUuid)
			{
				$("#activityType_uuid").val(activityTypeUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function editActivity(ActivityUuid)
{
	window.location.href = jumpPath + 'view/luckeys/activity/editActivity.jsp?ActivityUuid='+ActivityUuid;
}

function addFile(e)
{
	debugger;
	var id = e.parentElement.parentElement.getAttribute("id");
	$('#'+id).append('<li><input type="file" name="'+id+'"/><button type="button" class="btn btn-primary" onClick="clickHandler(this)">删除</button></li>');
}

function clickHandler(e){
	e.parentNode.remove();
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'activity/deleteActivity?activityUuid='+id;
		send(reqPath, "POST", "application/json", "json", "{}", function(res)
		{
			if (res.result == '1')
			{
				initActivityData(1,15);
			}
		});
	}
}

function addImageUrl(ActivityUuid)
{
	var url = jumpPath + 'view/luckeys/Activity/listActivityImage.jsp?ActivityUuid='+ActivityUuid;
	window.location.href = url;
}

function initActivityData(pageNumber,pagesize)
{
	var reqPath = basePath+'activity/queryPage';
	var sendBody = '{"curPage": 1,"pageSize": 15';
	var $tab = $('#activityTables tbody').html("");

	
	var reqPath = basePath + 'activity/queryPage';
	var sendBody = '{"curPage": '+pageNumber+',"pageSize": '+pagesize+'';
	var $tab = $('#activityTables tbody').html("");

	var requestData = $('#frm_activity').serializeObject();
	requestData = JSON.stringify(requestData);
	sendBody = sendBody+requestData.replace('{',',');
	
	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == '1')
		{
			if (res.data.data.length > 0)
			{
				debugger;
				var tpl_tr = $('#tpl_list').html(), rows = '';
				var sum = 1;
				if(pageNumber!=1)
				{
					sum = pagesize*(pageNumber-1)+1;
				}
				$.each(res.data.data, function(i,n)
				{
					var activityStatus = '结束';
					if(null!=n.activityStatus&&n.activityStatus==1)
					{
						activityStatus = '未开始';
					}else if (null!=n.activityStatus&&n.activityStatus==2)
					{
						activityStatus = '开始';
					}
					var chipsStatus = '未开始';
					if(null!=n.chipsStatus&&n.chipsStatus==1)
					{
						chipsStatus = '成功';
					}else if(null!=n.chipsStatus&&n.chipsStatus==2)
					{
						chipsStatus = '失败';
					}
					var operation="<button onclick='editActivity(\""+n.activityUuid+"\")' type='button' class='btn btn-primary'>编辑</button>"+
								  "<button onclick='del(\""+n.activityUuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					var ActivityName = "<button onclick='addImageUrl(\""+n.ActivityUuid+"\")' type='button' class='btn btn-primary'>"+n.ActivityName+"</button>";
					rows += tpl_tr.replace(/\$activityName/,n.activityName)
							      .replace(/\$activityTypeName/,n.activityTypeName)
							      .replace(/\$cityName/, n.cityName)
							      .replace(/\$totalPrice/, n.totalPrice)
							      .replace(/\$unitPrice/, n.unitPrice)
							      .replace(/\$bets/, n.bets)
							      .replace(/\$locationName/, n.locationName)
							      .replace(/\$activityStatus/, activityStatus)
							      .replace(/\$activityDay/, n.activityDay)
							      .replace(/\$chipsStatus/, chipsStatus)
							      .replace(/\$startDate/, getLocalTime(n.startDate1.toString().substr(0,10)))
							      .replace(/\$endDate/, getLocalTime(n.endDate.toString().substr(0,10)))
							      .replace(/\$userName/, n.userName)
							      .replace(/\$createDate/, getLocalTime(n.createDate.toString().substr(0,10)))
							      .replace(/\$operation/, operation);
					sum++;
				});
				$tab.append(rows);

				// page
				pagefunction(initActivityData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

function updateActivity()
{
	
	var requsetPath = basePath + "activity/updateActivity";
	var requestData = $('#frm_activity').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{
		if (resultData.result == '1')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/luckeys/activity/listActivity.jsp';
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}
